//
//  HuoDongDetailViewController.m
//  LoverGo
//
//  Created by liudonghuan on 14-9-4.
//
//

#import "LGUtil.h"
#import "HuoDongDetailViewController.h"
#import "OrderViewController.h"
@interface HuoDongDetailViewController ()

@end

@implementation HuoDongDetailViewController


-(id)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        _reciveData = [[NSDictionary alloc]initWithDictionary:dic];
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
-(void)loadView
{
    [self dinglan];
    self.joinDict = [[NSMutableDictionary alloc]init];
    _huoDongDetailView = [[HuoDongDetailView alloc]initWithFrame:CGRectMake(0, TOPBAR, SCREEN_WIDTH, SCREEN_HEIGHT-BOTTOMBAR)];
    _huoDongDetailView.hdDelegate = self;
    [self setView:_huoDongDetailView];
    [self refreshView:_reciveData];
    UIButton* rBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,30,30)];
    [rBtn setImage:[UIImage imageNamed:@"more_button_up.png"]forState:UIControlStateNormal];
    [rBtn setImage:[UIImage imageNamed:@"more_button_down.png"]forState:UIControlStateHighlighted];
    [rBtn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:rBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UITapGestureRecognizer *moreinfoTG = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moreinfoclick)];
    UITapGestureRecognizer *shangjiaTG = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shangjiaclick)];
    [_huoDongDetailView.moreInfo addGestureRecognizer:moreinfoTG];
    [_huoDongDetailView.shangJia addGestureRecognizer:shangjiaTG];

}
-(void)dinglan{
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,20, 20)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(dinglanBackBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [backBtn setShowsTouchWhenHighlighted:YES] ;
    UIBarButtonItem *backbutton = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backbutton;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, 140, 30)];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = LGCOLOR_blue;
    title.text = @"活动详情";
    self.navigationItem.titleView = title;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"barBG.png"] forBarMetrics:UIBarMetricsDefault];
}
-(void)dinglanBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)btnclick
{
    _asheet = [[UIActionSheet alloc]initWithTitle:@"您打算？" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"分享" otherButtonTitles:nil];
    _asheet.tag =1;
    [_asheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        UIImage *_shareImg = nil;
        if (_huoDongDetailView.activityHeadImage) {
            _shareImg = _huoDongDetailView.activityHeadImage.image;
        }
        [LGUtil shareToSNSURL:[NSString stringWithFormat:@"%@%@",@"http://123.57.7.162:8588/lover-go/",[_reciveData objectForKey:@"htmlName"]] title:[_reciveData objectForKey:@"title"] img:_shareImg content:[_reciveData objectForKey:@"title"]
                   controller:self];
    }
}

-(void)hdBtnOclick:(HuoDongDetailView *)view Button:(BtnType)type
{
    switch (type) {
        case BtnTypejoinActivity:{//报名参加活动
                [self JoinActivityStatus];
        }
            break;
        default:
            break;
    }

}

-(void)JoinActivityStatus
{
    _huoDongDetailView.joinActivity.enabled = NO;
    if ([[DataCenter getSingleton].userInfo uuid]==nil) {
        LoginViewController *vc = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        _huoDongDetailView.joinActivity.enabled = YES;
    }else{
    NSString *activityUuid = [_reciveData objectForKey:@"uuid"];
    NSString *orderUuid = [[NSUUID UUID] UUIDString];
    NSString *orderName = [_reciveData objectForKey:@"title"];
    NSString *appUserUuid = [[DataCenter getSingleton].userInfo uuid];
    NSString *name = [[DataCenter getSingleton].userInfo userName];
    NSString *tel = [[DataCenter getSingleton].userInfo mobile];
    NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:
                          orderUuid,@"activityOrderUuid",
                          activityUuid,@"activityUuid",
                          orderName,@"activityOrderName",
                          appUserUuid,@"appUserUuid",
                          name,@"appUserName",
                          tel,@"appUserMobile",
                          nil];
    [SVHTTPRequest POST:addActivityOrder
            parameters:para
            completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                if (error) {
                    [[[[UIApplication sharedApplication] delegate] window] makeToast:@"网络接连失败"
                                                                            duration:1.0
                                                                            position:@"center"];
                    return ;
                    _huoDongDetailView.joinActivity.enabled = YES;
                }
                NSError *jsonError = nil;
                id jsonObject = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&jsonError];
                if (jsonError) {
                    [[[[UIApplication sharedApplication] delegate] window] makeToast:@"数据解析异常"
                                                                            duration:1.0
                                                                            position:@"center"];
                    _huoDongDetailView.joinActivity.enabled = YES;
                }else{
                    _status = [[jsonObject objectForKey:@"status"] intValue];
                    switch (_status) {
                        case 0:{//成功
                            _joinDict = [[jsonObject objectForKey:@"data"] objectAtIndex:0];
                            JoinSureViewController *_joinSureV = [[JoinSureViewController alloc]initWithDic:_joinDict Time:[_reciveData objectForKey:@"actTime"] PayType:fromHuoDong];
                            self.navigationItem.backBarButtonItem = [LGUtil getBackButton];
                            [self.navigationController pushViewController:_joinSureV animated:YES];
                            _huoDongDetailView.joinActivity.enabled = YES;
                        }
                            break;
                        case 1://提交参数有空值  
                        {
                            [[[[UIApplication sharedApplication] delegate] window] makeToast:@"服务器拥挤请稍后重试"
                                                                                    duration:1.0
                                                                                    position:@"center"];
                            _huoDongDetailView.joinActivity.enabled = YES;
                            
                            break;
                        }
                        case 2://此订单已存在，查看待付款订单
                        {
                            [[[[UIApplication sharedApplication] delegate] window] makeToast:@"此订单已存在，请选择订单继续完成支付"
                                                                                    duration:2.5
                                                                                    position:@"center"];
                            OrderViewController *_huoDongDetailController = [[OrderViewController alloc]init];
                            self.navigationItem.backBarButtonItem = [LGUtil getBackButton];
                            [self.navigationController pushViewController:_huoDongDetailController animated:YES];
                            _huoDongDetailView.joinActivity.enabled = YES;
                            break;
                        }
                        case 3://活动参与人数已满，您来晚了
                            [[[[UIApplication sharedApplication] delegate] window] makeToast:@"活动参与人数已满，您来晚了"
                                                                                    duration:1.0
                                                                                    position:@"center"];
                            _huoDongDetailView.joinActivity.enabled = YES;
                            break;
                        case 4:
                            [[[[UIApplication sharedApplication] delegate] window] makeToast:@"您已参加过本期活动"
                                                                                    duration:1.0
                                                                                    position:@"center"];
                            _huoDongDetailView.joinActivity.enabled = YES;

                            break;
                        default:
                            break;
                    }

                }

            }];
    }
}

-(void)refreshView:(NSDictionary *)dic
{
    if ([[_reciveData objectForKey:@"canJoin"] intValue]!=1) {
        _huoDongDetailView.joinActivity.enabled = NO;
        [_huoDongDetailView.joinActivity setTitle:@"即将开放" forState:UIControlStateNormal];
        _huoDongDetailView.joinActivity.backgroundColor = [UIColor grayColor];
    }
    NSString *mainimage = [NSString stringWithFormat:@"%@%@%@",imagebaseUrl,baseUploadImages,[_reciveData objectForKey:@"indexImage"]];
    _huoDongDetailView.activityHeadImage.imageUrl = mainimage;
    _huoDongDetailView.activityTitle.text = [NSString stringWithFormat:@"%@%@",@"   ",[self DataIfNull:@"title"]]; 
    _huoDongDetailView.activityPrice.itemtext.text = [[NSString alloc]initWithFormat:@"%@%@",[self DataIfNull:@"price"],@"元"];
    _huoDongDetailView.activityOpenTime.itemtext.text = [self DataIfNull:@"actTime"];
    UITapGestureRecognizer *readtime = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(readTimeMore)];
    [_huoDongDetailView.activityOpenTime addGestureRecognizer:readtime];
    
}
-(void)moreinfoclick
{
    self.navigationItem.backBarButtonItem = [LGUtil getBackButton];
    NSString *yuanstring = [_reciveData objectForKey:@"uuid"];
    
    NSString *weburl = [NSString stringWithFormat:@"%@%@",@"http://lover-go.com:8588/lover-go/phoneActivity/findActivityByUuidForView?activityUuid=",yuanstring];
    WebViewController *wevc = [[WebViewController alloc]initWithString:weburl Title:@"活动详情"];
    [self.navigationController pushViewController:wevc animated:true];
}
-(void)shangjiaclick
{
    NSString* para = [_reciveData objectForKey:@"placeUuid"];
    ShangJiaViewController *_shangJiaViewController = [[ShangJiaViewController alloc]initWithDic:para];
    self.navigationItem.backBarButtonItem = [LGUtil getBackButton];
    [self.navigationController pushViewController:_shangJiaViewController animated:YES];
}
-(void)readTimeMore
{
    [self.view makeToast:[self DataIfNull:@"actTime"]
                duration:3.0
                position:@"top"
                   title:@"活动时间"];
}
-(NSString *)DataIfNull:(NSString *)key
{
    NSString *dataString = [[NSString alloc]initWithFormat:@"%@",[_reciveData objectForKey:key]];
    if (![dataString compare:@""]) {
        dataString = @"小编正在收集此项数据中哦...";
    }
    return dataString;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"活动详情"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"活动详情"];
}

@end
